﻿using AutoMapper;
using BW.EatRabbit.API.Application.Commands;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using BW.EatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace BW.EatRabbit.API.Application.CommandHandlers
{
    /// <summary>
    /// 逻辑删除命令处理
    /// </summary>
    public class BasicInformationDeleteCommandHandler : IRequestHandler<BasicInformationDeleteCommand, APIResult<int>>
    {
        /// <summary>
        /// 基本信息仓储
        /// </summary>
        private readonly IBaseRepository<BasicInformation> basicinformationRepository;
        /// <summary>
        /// 映射automapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="basicinformationRepository">基本信息仓储</param>
        /// <param name="mapper">映射automapper</param>
        public BasicInformationDeleteCommandHandler(IBaseRepository<BasicInformation> basicinformationRepository, IMapper mapper)
        {
            this.basicinformationRepository = basicinformationRepository;
            this.mapper = mapper;

        }
        public Task<APIResult<int>> Handle(BasicInformationDeleteCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = ResultCode.删除成功;
            result.Message = ResultCode.删除成功.ToString();

            BasicInformation basic = mapper.Map<BasicInformation>(request);
            basic.UpdatedTime = DateTime.Now;
            basic.IsDeleted = true;
            basicinformationRepository.UpdateEntity(basic);



            return Task.FromResult(result);
        }
    }
}
